<template>
  <el-form
    ref="auditForm"
    :model="form"
    :size="size"
    :rules="rules"
    :label-suffix="labelSuffix"
    :label-position="labelPosition"
    :label-width="labelWidth"
    @submit.native.prevent
  >
    <el-form-item label="审核操作">
      <el-radio-group v-model="form.allow">
        <el-radio-button label="1">通过</el-radio-button>
        <el-radio-button label="-1">拒绝</el-radio-button>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      label="操作备注"
      prop="mark"
    >
      <el-input
        v-model="form.mark"
        type="textarea"
      />
    </el-form-item>
    <slot name="action">
      <form-action v-if="actionVisible">
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
          >{{ submitButtonText }}</el-button>
        </el-form-item>
      </form-action>
    </slot>
  </el-form>
</template>

<script>
import FormAction from '@/components/Form/FormAction'
export default {
  name: 'AuditForm',
  components: {
    FormAction
  },
  props: {
    primaryKey: {
      type: [String, Number],
      default: 0
    },
    auditType: {
      type: String,
      default: ''
    },
    labelPosition: {
      type: String,
      default: 'top'
    },
    labelWidth: {
      type: String,
      default: '20%'
    },
    labelSuffix: {
      type: String,
      default: '：'
    },
    size: {
      type: String,
      default: 'small'
    },
    submitButtonText: {
      type: String,
      default: '提交保存'
    },
    actionVisible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      form: {
        mark: '',
        handle: '',
        allow: 1
      },
      rules: {
        mark: [{ required: true, message: '请输入操作备注', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    console.log('on mounted form')
  },
  methods: {
    onSubmit() {
      this.$refs.auditForm.validate((valid, obj) => {
        if (valid) {
          console.log(this.form)
          this.$http.book.audit(this.form, this.primaryKey).then(res => {
            this.$emit('submit', res.data)
          })
        } else {
          let firstErrorElname = ''
          for (const i in obj) {
            firstErrorElname = `formInput.${i}`
            break
          }
          var dom = this.$refs[firstErrorElname]
          console.log(dom, this.$refs)
          if (dom) {
            dom.$refs.input && dom.$refs.input.focus()
            dom.$el.scrollIntoView({
              // 滚动到指定节点
              block: 'center', // 值有start,center,end，nearest，当前显示在视图区域中间
              behavior: 'smooth' // 值有auto、instant,smooth，缓动动画（当前是慢速的）
            })
          }
          console.log('error submit!!', this.form)
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.select-item {
  .el-select-dropdown__item {
    height: auto;
    line-height: 1;
    padding-top: 8px;
    padding-bottom: 8px;
  }
  .freight-header {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #333;
    span {
      margin-left: 5px;
      font-style: italic;
      font-size: 12px;
    }
  }
  .freight-address {
    padding-top: 5px;
    font-size: 12px;
    color: #999;
  }
}
</style>

